'use strict'

const util = {
    /**
     * 设置token
     *
     * @param  {[type]} token [description]
     * @return {[type]}       [description]
     */
    setToken(token, name = 'login_token')
    {
        localStorage.setItem(name, token)
    },

    /**
     * 获取token
     *
     * @return {[type]} [description]
     */
    getToken(name = 'login_token')
    {
        let token = localStorage.getItem(name)
        if(token){
            return token
        }

        return false;
    },

    /**
     * 删除token
     *
     * @return {[type]} [description]
     */
    unsetToken(name = 'login_token')
    {
        localStorage.removeItem(name)
    },

    /**
     * 设置titla
     *
     * @param  {[type]} title [description]
     * @return {[type]}       [description]
     */
    setTitle(title)
    {
        window.document.title = title || 'myApp';
    },

    /**
     * 格式化时间戳
     *
     * @param  {[type]} timeStamp [description]
     * @return {[type]}           [description]
     */
    formatDate(timeStamp, type = 0)
    {
        const date =  new Date(parseInt(timeStamp) * 1000);
        const y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        let i = date.getMinutes();
        i = i < 10 ? ('0' + i) : i;
        let s = date.getSeconds();
        s = s < 10 ? ('0' + s) : s;

        let formatDate = ''
        switch(type)
        {
            case 1:
                // Y-m-d H:i:s
                formatDate = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
                break;
            case 2:
                // Y-m-d H:i
                formatDate = y + '-' + m + '-' + d + ' ' + h + ':' + i
                break;
            default:
                // Y-m-d
                formatDate = y + '-' + m + '-' + d
                break;
        }

        return formatDate
    },

    /**
     * 数组对象转uri参数
     *
     * @param  {[type]} param  [description]
     * @param  {[type]} key    [description]
     * @param  {[type]} encode [description]
     * @return {[type]}        [description]
     */
    urlEncode(param, key, encode)
    {
        if(param==null){
            return '';
        }
        let paramStr = '';
        let t = typeof (param);
        if(t == 'string' || t == 'number' || t == 'boolean'){
            paramStr += '&' + key + '='  + ((encode == true) ? encodeURIComponent(param) : param); 
        }
        else{
            for(let i in param)
            {
                let k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i)
                paramStr += urlEncode(param[i], k, encode)
            }
        }

        return paramStr;
    },

    /**
     * 判断客户端类型
     *
     * @return {Boolean} [description]
     */
    isMoble(){
        if((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))){
            return true
        }
        
        return false
    },

    /**
     * 金额转大写
     *
     * @param  {[type]} num [description]
     * @return {[type]}     [description]
     */
    money2Chinese(num){
        if(typeof num) throw new Error('参数为数字')
        let strOutput = ""
        let strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分'
        num += "00"
        const intPos = num.indexOf('.')
        if(intPos >= 0) {
            num = num.substring(0, intPos) + num.substr(intPos + 1, 2)
        }
        strUnit = strUnit.substr(strUnit.length - num.length)
        for(let i = 0; i < num.length; i++) {
            strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i, 1), 1) + strUnit.substr(i, 1)
        }

        return strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元");
    }

}

export default util;
